package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/report")
public class ReportController {
    @Reference
    MemberService memberService;

    @Reference
    OrderService orderService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            Map<String,Object> map =memberService.getMemberReport();
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            Map<String,Object> map=orderService.getSetmealReport();
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map<String,Object> map=memberService.getBusinessReportData();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @RequestMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String,Object> map=memberService.getBusinessReportData();
        String temlateRealPath = "D:/ideawork/health_parent/health_web/src/main/webapp/template/report_template.xlsx";
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFRow row = sheet.getRow(2);
        XSSFCell cell = row.getCell(5);
        cell.setCellValue((String) map.get("reportDate"));

        Integer todayNewMember = (Integer) map.get("todayNewMember");
        sheet.getRow(4).getCell(5).setCellValue(todayNewMember);

        Integer totalMember = (Integer) map.get("totalMember");
        sheet.getRow(4).getCell(7).setCellValue(totalMember);

        Integer thisWeekNewMember = (Integer) map.get("thisWeekNewMember");
        sheet.getRow(5).getCell(5).setCellValue(thisWeekNewMember);

        Integer thisMonthNewMember = (Integer) map.get("thisMonthNewMember");
        sheet.getRow(5).getCell(7).setCellValue(thisMonthNewMember);


        Integer todayOrderNumber = (Integer) map.get("todayOrderNumber");
        sheet.getRow(7).getCell(5).setCellValue(todayOrderNumber);

        Integer todayVisitsNumber = (Integer) map.get("todayVisitsNumber");
        sheet.getRow(7).getCell(7).setCellValue(todayVisitsNumber);

        Integer thisWeekOrderNumber = (Integer) map.get("thisWeekOrderNumber");
        sheet.getRow(8).getCell(5).setCellValue(thisWeekOrderNumber);

        Integer thisWeekVisitsNumber = (Integer) map.get("thisWeekVisitsNumber");
        sheet.getRow(8).getCell(7).setCellValue(thisWeekVisitsNumber);

        Integer thisMonthOrderNumber = (Integer) map.get("thisMonthOrderNumber");
        sheet.getRow(9).getCell(5).setCellValue(thisMonthOrderNumber);

        Integer thisMonthVisitsNumber = (Integer) map.get("thisMonthVisitsNumber");
        sheet.getRow(9).getCell(7).setCellValue(thisMonthVisitsNumber);

        List<Map<String,Object>> hotSetmeal = (List<Map<String, Object>>) map.get("hotSetmeal");

        int i =12;
        for (Map<String, Object> stringObjectMap : hotSetmeal) {
            String name = (String) stringObjectMap.get("name");
            sheet.getRow(i).getCell(4).setCellValue(name);
            Long setmeal_count = (Long) stringObjectMap.get("setmeal_count");
            sheet.getRow(i).getCell(5).setCellValue(setmeal_count);
            BigDecimal proportion = (BigDecimal) stringObjectMap.get("proportion");
            sheet.getRow(i).getCell(6).setCellValue(proportion.doubleValue());
                i++;
        }


        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("content-Disposition","attachment;filename=report.xlsx");
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        workbook.close();

    }
}
